package com.coolgor.axnut.web.mdmis;

import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.axnut.util.HttpServletRequestUtil;
import com.coolgor.axnut.dto.mdmis.PolicyMonitorExecution;
import com.coolgor.axnut.entity.mdmis.PolicyMonitor;
import com.coolgor.axnut.service.mdmis.PolicyMonitorService;


@Controller
@RequestMapping(value="mdmis", method={RequestMethod.GET,RequestMethod.POST})
public class PolicyMonitorController {
	@Autowired
	private  PolicyMonitorService policyMonitorService;
	
	@RequestMapping(value = "/getPolicyMonitor", method={RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getPolicyMonitor(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		PolicyMonitor policyMonitor = null;
	    String insureNo = HttpServletRequestUtil.getString(request, "insure_no");
	    if (!"".equals(insureNo) && insureNo!=null) {
	    	policyMonitor = policyMonitorService.getPolicyMonitor(insureNo);
			if (null != policyMonitor ) {
				//数据库中获取到 policy monitor 信息时执行如下
				try {
					modelMap.put("success", true);
					modelMap.put("data",policyMonitor);
				} catch (Exception e) {
					modelMap.put("success", false);
					modelMap.put("errMsg", e.toString());
				}
			} else {
				//数据库中没有该保单的monitor信息时执行如下
				modelMap.put("success", true);
				//返回一个空的信息回到页面
				modelMap.put("info", "null");
			}
	    } else {
	    	modelMap.put("success", false);
			modelMap.put("errMsg", "Empty insure NO");
	    }	    
		return modelMap;
	}	
	
	@RequestMapping(value = "/markPolicyMonitor", method={RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> markPolicyMonitor(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    String insureNo = HttpServletRequestUtil.getString(request, "insure_no");
	    if (!"".equals(insureNo) && insureNo!=null) {
			PolicyMonitor policyMonitor = policyMonitorService.getPolicyMonitor(insureNo);
		    Integer warnFlag = HttpServletRequestUtil.getInt(request, "warn_flag");
		    String warnInfo = HttpServletRequestUtil.getString(request, "warn_info");
		    Integer holdFlag = HttpServletRequestUtil.getInt(request, "hold_flag");
		    String holdInfo = HttpServletRequestUtil.getString(request, "hold_info");
		    PolicyMonitorExecution exe = null;
			if (policyMonitor == null) {
				//如果当前保单不存在警告及中断信息 则新增
				policyMonitor = new PolicyMonitor();
				policyMonitor.setInsureNo(insureNo);
				//警告信息
				if (warnFlag>=0 && warnFlag!=null) {
					policyMonitor.setWarnFlag(warnFlag);			    	
			    } else {
			    	policyMonitor.setWarnFlag(0);
			    }			    
			    if (!"".equals(warnInfo) && warnInfo!=null) {
					policyMonitor.setWarnInfo(warnInfo);			    	
			    }
			    //中断信息
			    if (holdFlag>=0 && holdFlag!=null) {
					policyMonitor.setHoldFlag(holdFlag);			    	
			    } else {
			    	policyMonitor.setHoldFlag(0);
			    }			    
			    if (!"".equals(holdInfo) && holdInfo!=null) {
					policyMonitor.setHoldInfo(holdInfo);			    	
			    }
				policyMonitor.setCreateTime(new Date());
				policyMonitor.setLastEditTime(new Date());
				exe = policyMonitorService.addPolicyMonitor(policyMonitor);
			} else {
				//如果当前保单已经存在警告及中断信息 则修改
				//警告信息
			    if (warnFlag>=0 && warnFlag!=null) {
					policyMonitor.setWarnFlag(warnFlag);			    	
			    } else {
			    	policyMonitor.setWarnFlag(0);	
			    }
			    if (!"".equals(warnInfo) && warnInfo!=null) {
					policyMonitor.setWarnInfo(warnInfo);			    	
			    }
				//中断信息
			    if (holdFlag>=0 && holdFlag!=null) {
					policyMonitor.setHoldFlag(holdFlag);			    	
			    } else {
			    	policyMonitor.setHoldFlag(0);	
			    }
			    if (!"".equals(holdInfo) && holdInfo!=null) {
					policyMonitor.setHoldInfo(holdInfo);			    	
			    }
				policyMonitor.setLastEditTime(new Date());	
				exe = policyMonitorService.modifyPolicyMonitor(policyMonitor);
			}
	    	if (null != exe ) {
				try {
//					modelMap.put("total",exe.getCount());
					modelMap.put("data",exe.getPolicyMonitor());
					modelMap.put("success", true);
				} catch (Exception e) {
					modelMap.put("success", false);
					modelMap.put("errMsg", e.toString());
				}
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", "Operation Error");
			}
	    } else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty Parameter");
		}
		return modelMap;
	}		
	
	@RequestMapping(value = "/removePolicyMonitor", method={RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> removeMonitor(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String insureNoString = HttpServletRequestUtil.getString(request, "insureNos");
		List<PolicyMonitor> list = new ArrayList<PolicyMonitor>();
		PolicyMonitor policyMonitor ;
	    if (!"".equals(insureNoString) && insureNoString!=null) {
	    	String[] insureNos = insureNoString.split(",");
	    	for (int i=0; i<insureNos.length; i++) {
	    		policyMonitor = new PolicyMonitor();
	    		policyMonitor.setInsureNo(insureNos[i]);
	    		list.add(policyMonitor);
	    	}
	    	PolicyMonitorExecution exe = policyMonitorService.batchRemovePolicyMonitor(list);
	    	if (null != exe ) {
				try {
					modelMap.put("total",exe.getCount());
					modelMap.put("success", true);
				} catch (Exception e) {
					modelMap.put("success", false);
					modelMap.put("errMsg", e.toString());
				}
			} else {
				modelMap.put("success", false);
				modelMap.put("errMsg", "Empty List");
			}
	    } else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty Parameter");
		}
		return modelMap;
	}		
	
}
